#include "LoginMediator.h"

#include "simplepuremvcexample.h"

#include "LoginVO.h"
#include "LoginProxy.h"

#include "AppFacade.h"

#include "ConstString.h"

/**
 * @brief LoginMediator构造函数
 * 初始化父类Mediator，设置中介者名称和视图组件，并连接登录按钮信号
 * @param view 登录视图组件指针
 */
LoginMediator::LoginMediator(void const* view)
    :Mediator(LOGIN_MEDIATOR_NAME, view)
{
    simplePureMVCExample * loginView =(simplePureMVCExample *)view;
    connect(loginView->ui.pushButton,SIGNAL(clicked()),this,SLOT(onLogin()));
}

/**
 * @brief LoginMediator析构函数
 */
LoginMediator::~LoginMediator(void)
{
}

/**
 * @brief 获取感兴趣的通知列表
 * 返回中介者需要处理的通知名称列表，这里关注登录成功通知
 * @return 包含通知名称的列表
 */
Mediator::NotificationNames LoginMediator::listNotificationInterests(void) const
{
    typedef StdContainerAggregate<std::list<NotificationNames::element_type::type> > result_t;
    result_t* result = new result_t;
    result->get().push_back(STR_NOTIFY_LOGIN_SUC);
    return NotificationNames(result);
}

/**
 * @brief 处理接收到的通知
 * 根据通知名称处理不同逻辑，登录成功时更新UI显示登录结果
 * @param notification 通知对象，包含通知名称和数据
 */
void LoginMediator::handleNotification(INotification const& notification)
{
    std::string notifinName = notification.getName();
    
    if (STR_NOTIFY_LOGIN_SUC == notifinName)
    {
        LoginProxy* proxy = dynamic_cast<LoginProxy*>(&AppFacade::getInstance().retrieveProxy(LOGIN_PROXY_NAME));
        std::string loginRst = proxy->GetLoginRst();

        simplePureMVCExample * loginView =(simplePureMVCExample *)getViewComponent();
        loginView->ui.label_3->setText(QString::fromStdString(loginRst));
    }

}

/**
 * @brief 登录按钮点击事件处理函数
 * 从UI获取用户名和密码，创建LoginVO对象并发送登录通知
 */
void LoginMediator::onLogin()
{
    simplePureMVCExample * loginView =(simplePureMVCExample *)getViewComponent();

    QString strUserName = loginView->ui.lineEdit->text();
    QString strUserpassward = loginView->ui.lineEdit_2->text();

    std::string strStdUserName = strUserName.toStdString();
    std::string strStdUserpassward = strUserpassward.toStdString();

    data::LoginVO loginVale;
    loginVale.strUsername = strStdUserName;
    loginVale.strpassward = strStdUserpassward;

    IFacade &facade = AppFacade::getInstance();
    facade.sendNotification(STR_NOTIFY_LOGIN,&loginVale);
}
